import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ArrayUtils {
    public  static int[][] JSON2Array(String json) {
        int col = 0;
        int row = 0;
        if (!json.startsWith("[[")) return null;
        String substring = json.substring(1, json.length() - 2);
        String[] split = substring.split("],\\[");
        row = split.length;
        split[0] = split[0].substring(1);
        split[0] = "[" + split[0] + "]";
        JSONArray parse = (JSONArray)JSON.parse(split[0]);
        col = parse.toArray().length;
        int [][] res = new int[row][col];
        setArry(parse.toArray(),res[0]);
        for (int i =1;i< split.length;i++) {
            Object[] objects = JSONArray.parseArray("[" + split[i] + "]").toArray();
            setArry(objects,res[i]);

        }
        return res;
    }
    public static void setArry(Object[] org,int [] res) {
        for (int i= 0;i<org.length;i++) {
            res[i] = (int) org[i];
        }
    }
    public static Message jsonToMessage(String json){
        try {
            JSONObject parse = (JSONObject)JSON.parse(json);
            Message message = new Message();
            message.setContent(null==parse.get("content")?null:parse.get("content").toString());
            message.setEventName(parse.get("eventName").toString());
            message.setType((int)parse.get("type"));
            return message;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
